﻿Public Class frmTienIch

    Private tiBUS As TienIchBUS
    Private tiDTO As TienIchDTO
    Private cm As CurrencyManager
    Private flagIsUp As Boolean

    Private Sub frmTienIch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
    End Sub
    Private Sub LoadData()
        dgvTienIch.ClearSelection()
        tiBUS = New TienIchBUS
        Dim tiList = New List(Of TienIchDTO)
        tiList = tiBUS.GetData()
        cm = Me.BindingContext(tiList)
        txtmatienich.DataBindings.Add("Text", tiList, "MaTienIch")
        txtnoidung.DataBindings.Add("Text", tiList, "NoiDungTienIch")
        dgvTienIch.DataSource = tiList
    End Sub
    Private Sub LockTextbox(ByVal f As Boolean)
        txtmatienich.Enabled = f
        txtnoidung.Enabled = f
     
        If f = False Then
            txtmatienich.BackColor = Color.LemonChiffon
            txtnoidung.BackColor = Color.LemonChiffon

        Else
            txtmatienich.BackColor = Color.White
            txtnoidung.BackColor = Color.White
           
        End If
    End Sub
    Private Sub LockButton(ByVal dt As Boolean)
        btnThem.Enabled = dt
        btnSua.Enabled = dt
        btnXoa.Enabled = dt
        btnLuu.Enabled = Not dt
        btnThoat.Enabled = dt
    End Sub
    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        flagIsUp = True
        LockTextbox(True)
        LockButton(False)
        ClearTextbox()
        dgvTienIch.Enabled = False
        txtmatienich.Focus()
    End Sub
    Private Sub ClearTextbox()
        txtmatienich.Text = ""
        txtnoidung.Text = ""
      
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim cmnd As String = txtmatienich.Text
        Dim qs = MsgBox("Bạn chắc chắn muốn xóa tất cả thông tin về:" _
                        & vbCrLf & vbCrLf & " - Mã Tiện Ích: " & txtmatienich.Text _
                        & vbCrLf & vbCrLf & " - Nội Dụng Tiện Ích: " & txtnoidung.Text, _
                        vbInformation + vbYesNo, "Thông báo")
        If qs = vbYes Then
            tiDTO = New TienIchDTO(cmnd)
            tiBUS = New TienIchBUS()
            tiBUS.XoaTienIch(tiDTO)
            ClearBinding()
            LoadData()
            LockTextbox(False)
            LockButton(True)
            dgvTienIch.Enabled = True
            MsgBox("Dữ liệu đã xóa thành công", vbInformation, "Thông báo")
        Else
            MsgBox("Đã hủy thao tác xóa!", vbExclamation, "Thông báo")
        End If
    End Sub
    Private Sub ClearBinding()
        txtmatienich.DataBindings.Clear()
        txtnoidung.DataBindings.Clear()
       

    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        flagIsUp = False
        LockTextbox(True)
        LockButton(False)
        dgvTienIch.Enabled = False
        txtmatienich.Focus()
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        ' khai bao bien
        Dim matienich As String = txtmatienich.Text
        Dim ndtienich As String = txtnoidung.Text
      
        '---------------------------------------------
        If TestInfo() Then
            tiDTO = New TienIchDTO(matienich, ndtienich)
            tiBUS = New TienIchBUS()
            tiBUS.CapNhat(tiDTO, flagIsUp)
            ClearBinding()
            LoadData()
            LockTextbox(False)
            LockButton(True)
            dgvTienIch.Enabled = True
            MsgBox("Đã cập nhật dữ liệu thành công", MsgBoxStyle.Information, "Thông báo")
        End If

    End Sub
    Private Function TestInfo() As Boolean
        TestInfo = True
        If Trim(txtmatienich.Text) = "" Then
            TestInfo = False
            MsgBox("Mã Tiện Ích không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtmatienich.Focus()
            Exit Function
        ElseIf Trim(txtnoidung.Text) = "" Then
            TestInfo = False
            MsgBox("Nội Dung không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtnoidung.Focus()
            Exit Function
     
        End If
    End Function

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        ClearBinding()
        LoadData()
        LockTextbox(False)
        LockButton(True)
        dgvTienIch.Enabled = True
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub
End Class